/**
 * Created by bjh on 2017/11/05.
 *
 */
import GM from "g2-mobile";

function enhancedPoint() {
    let yep = true;
    //空心圆的points样式注册
    const drawHollowCircle = function (cfg, canvas, style) {
        const ctx = canvas.getContext('2d');
        const { points, x, y, size, color } = cfg;

        ctx.beginPath();
        ctx.arc(x, y, size, 0, 2 * Math.PI);
        ctx.fillStyle = "#fff"
        ctx.strokeStyle = color;
        ctx.fill();
        ctx.stroke();
    }

    const drawRoundRect = function (cfg, canvas, style) {
        const ctx = canvas.getContext('2d');
        let { points, x, y, size, color } = cfg;
        let w = canvas.width * points.size,
            r = 2,
            h = (canvas.height - 20 - 20 ) * points.y;
        x = x - w / 2;
        ctx.beginPath();
        if ( h < r ){
            r = h;
        }
        //直线加一个小圆弧的逻辑来添加圆角
        ctx.moveTo(x + r, y + r);
        ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 3 / 2)
        ctx.lineTo(x + w - r, y);
        ctx.arc(x + w - r, y + r, r, Math.PI * 3 / 2, Math.PI * 2);
        ctx.lineTo(x + w, y + r);
        ctx.lineTo(x + w, y + h + 1); // +1 是为了在柱状图没有值的时候显示出来 不会有空缺的感觉
        ctx.lineTo(x, y + h + 1);
        ctx.lineTo(x, y + r);
        ctx.fillStyle = color
        ctx.fill();
    };

    //自定义绘制数据的的形状

    try {
        GM.Shape.registShape('point', 'hollowCircle', {
            getShapePoints: function (cfg) {
                return cfg;
            },
            drawShape: (cfg, canvas) => {
                var style = cfg.style || {};
                drawHollowCircle(cfg, canvas, style);
            }
        });
        GM.Shape.registShape('interval', 'roundRect', {
            getShapePoints: function (cfg) {
                return cfg
            },
            drawShape: function (cfg, canvas) {
                var style = cfg.style || {};
                drawRoundRect(cfg, canvas, style);
            }
        });
    } catch (err) {
        yep = false;
        console.log("自定义图形注册失败,错误：", err);
    }

    if (yep) {
        return true;
    }
    return false;

}

function enhancedPie() {

}

export default {
    enhancedPoint,
    enhancedPie,
} 